package UILayer;

import ControllerLayer.*;
import ModelLayer.*;
/**
 * Class responsible for the CustomerUI.
 * 
 * @author Anders A.P., Mie P., Anna K., Jakob K., Danny P.
 * 
 * @version 1.0
 */
public class CustomerUI
{
    private CustomerCtr customerCtr;
    private InputDialog inputDialog;
    /**
     * Constructor for objects of class CustUI
     */
    public CustomerUI() {
        customerCtr = new CustomerCtr();
        inputDialog = new InputDialog();

    }

    /**
     * Method which initiates the UI for handling the details of a customer.
     */
    public void startCustomerUI() {
        boolean goOn = true;
        MenuList<Integer> m1 = new MenuList<Integer>("\fKunde menu:", "\nVælg: ");
        m1.addOption("1", "Opret kunde", 1);
        m1.addOption("2", "Print kundeinfo", 2);
        m1.addOption("3", "Opdater kundeinformation", 3);
        m1.addOption("4", "Slet kunde", 4);
        m1.addOption("0", "Exit", 0);

        while(goOn)
        {
            Integer choice = m1.display();
            switch(choice){
                case 0: 
                goOn = false;
                break;
                
                case 1:
                createCustomer();
                break;
                
                case 2:
                retrieveCustomer();
                break;
                
                case 3:
                updateCustomer();
                break;
                
                case 4:
                deleteCustomer();
                break;
                
                default: 
                System.out.println("Input ikke genkendt");
                break;
            }
        }
    }

    /**
     * Creates a customer using inputs from the user.
     */
    private void createCustomer() {
       try {
    	   customerCtr.createCustomer(inputDialog.inputString("Kundens navn: "),
    	            inputDialog.inputString("Kundens addresse: "),  
    	            inputDialog.inputString("Kundens e-mail: "),
    	            inputDialog.inputString("Kundens telefonnummer: "));
       }       
       catch (Exception e){
    	   System.out.println(e);
       }
    }

    /**
     * Retrieves a customer object to the UI layer, allowing the user to interact with the object.
     */
    private void retrieveCustomer() {
        try {
            Customer c = customerCtr.getCustomer(inputDialog.inputInt("Kundens ID: "));
            System.out.printf("Kundens ID:\t\t%d\n", c.getCustomerNo());
            System.out.printf("Kundens navn:\t\t%s\n", c.getName());
            System.out.printf("Kundens addresse:\t%s\n", c.getAddress());
            System.out.printf("Kundens e-mail:\t\t%s\n", c.getEmail());
            System.out.printf("Kundens tlf:\t\t%s\n", c.getTlf());
            inputDialog.inputWait();
        }
        catch (Exception e) {
            System.out.println(e);
            inputDialog.inputWait();
        }
    }

    /**
     * Retrieves and updates the information in a customer object.
     */
    private void updateCustomer() {
        try {
            int id = inputDialog.inputInt("Kundens ID: ");
            Customer c = customerCtr.getCustomer(id);
            customerCtr.updateCustomer(id,
                inputDialog.updateString("navn: ", c.getName()),
                inputDialog.updateString("addresse: ", c.getAddress()),
                inputDialog.updateString("e-mail: ", c.getEmail()),
                inputDialog.updateString("telefon: ", c.getTlf()));
            inputDialog.inputWait();        
        }
        catch (Exception e) {
            System.out.println(e);
            inputDialog.inputWait();
        }
    }

    /**
     * Deletes a Customer object from the collection.
     */
    private void deleteCustomer() {
        try {
            customerCtr.deleteCustomer(inputDialog.inputInt("Kundens ID: "));
            inputDialog.inputWait();
        }
        catch (Exception e) {
            System.out.println(e);
            inputDialog.inputWait();
        }
    }
}

